const Blog = require("../model/BlogModel");

class BlogDao {
  async save(data) {
    const res = await Blog.create(data);
    return res.toJSON();
  }

  async update(data) {
    let { Id, Title, ImgUrl, BlogId, Content, Tags, Description, Status } =
      data;
    const res = await Blog.update(
      {
        Title,
        ImgUrl,
        BlogId,
        Content,
        Tags,
        Description,
        Status,
      },
      {
        where: {
          Id,
        },
      }
    );
    const r = await this.getOneById(Id);
    return r;
  }

  async delete(Id) {
    const res = await Blog.destroy({
      where: {
        Id,
      },
    });
    return true;
  }

  async getOneById(Id) {
    const res = await Blog.findByPk(Id);
    if (res?.dataValues) {
      return res.toJSON();
    }

    return null;
  }
}
module.exports = new BlogDao();
